package net.shizhibao.www.mylibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tencent.smtt.sdk.QbSdk;

import net.shizhibao.www.mylibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/7/12.
 */

public class DragDownPops {
    private static PopupWindow popupWindow;
    private static Activity context;

    public static void show(Activity ctx, View view, List<DragDownBean> list, final OnItemClickListener listener){
        context  = ctx;
        final View popView = LayoutInflater.from(context).inflate(
                R.layout.dragpops, null);
        ListView listView=popView.findViewById(R.id.listView);
        listView.setAdapter(new Dragadapter(ctx,list));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (popupWindow!=null){
                    popupWindow.dismiss();
                }
                if (listener!=null){
                    listener.onItemClick(position);
                }
            }
        });
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

//view为填充后得到的View实例

        popView.measure(w, h);
        popupWindow = new PopupWindow(popView, popView.getMeasuredWidth()
                , ViewGroup.LayoutParams.WRAP_CONTENT,true);
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口

        popupWindow.showAsDropDown(view);
        // Gravity.NO_GRAVITY, location[0], location[1] - getMeasureHeight());
    }

    public static void hide(){
        if (popupWindow!=null){
            popupWindow.dismiss();
        }
    }
    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public static class Dragadapter extends BaseAdapter {
        private Context context;
        private List<DragDownBean> list;
        LayoutInflater inflater;
        public Dragadapter(Context context, List list) {
            this.context = context;
            this.list = list;
            inflater = LayoutInflater.from(this.context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public  Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                convertView = inflater.inflate(R.layout.dragpopitems,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.drag_title.setText(list.get(position).getTitle());
            return convertView;
        }

        class ViewHolder{
            TextView drag_title;
            public ViewHolder(View view){
                drag_title = view.findViewById(R.id.drag_title);
            }
        }
    }

    public static class DragDownBean{
        private int id;
        private String name;
        private String title;

        public DragDownBean(int id, String name, String title) {
            this.id = id;
            this.name = name;
            this.title = title;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
        public static List<DragDownBean> get(){
            return get(true);
        }
        public static List<DragDownBean> get(boolean edit){
            List<DragDownBean> list = new ArrayList<>();
            if (edit){
                list.add(new DragDownBean(1,"edit","修改项目信息"));
            }

            list.add(new DragDownBean(2,"stop","结束项目"));
            return list;
        }
        public static List<DragDownBean> get2(){
            //
            List<DragDownBean> list = new ArrayList<>();
            list.add(new DragDownBean(1,"edit","修改项目信息"));
            list.add(new DragDownBean(2,"stop","结束项目"));
            return list;
        }
        public static List<DragDownBean> get3(){

            return get3(true);
        }
        public static List<DragDownBean> get3(boolean edit){
            List<DragDownBean> list = new ArrayList<>();
            if (edit){
                list.add(new DragDownBean(1,"edit","修改"));
            }

            list.add(new DragDownBean(2,"cancle","取消发布"));
            return list;
        }
        public static List<DragDownBean> get4(){
            List<DragDownBean> list = new ArrayList<>();
            list.add(new DragDownBean(1,"refresh","刷新"));

            list.add(new DragDownBean(2,"copy","复制链接"));

            list.add(new DragDownBean(3,"search","搜索"));

            list.add(new DragDownBean(4,"openother","其他应用打开"));

            list.add(new DragDownBean(5,"input","输入网址"));
            list.add(new DragDownBean(6,"close","关闭页面"));
            return list;
        }
    }
    public static void showPop(Activity activity, View bar_right, final BrowserOpera browserOpera){
        final List<DragDownBean> dragDownBeen = DragDownBean.get4();
        show(activity, bar_right, dragDownBeen, new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (browserOpera!=null){
                    switch (dragDownBeen.get(position).getName()){
                        case "refresh":
                            browserOpera.refresh();
                            break;
                        case "copy":
                            browserOpera.copy();
                            break;
                        case "search":
                            browserOpera.search();
                            break;
                        case "openother":
                            browserOpera.openother();
                            break;
                        case "input":
                            browserOpera.input();
                            break;
                        case "close":
                            browserOpera.close();
                            break;
                    }
                }
            }
        });

    }

}
